package com.eblacorp.archive.view.bean.realtydocument;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.SerializationUtils;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

import com.eblacorp.archive.domain.fn.RealtyDocument;
import com.eblacorp.archive.service.RealtyDocumentService;
import com.eblacorp.archive.view.utils.FacesUtils;

@ManagedBean
@ViewScoped
public class RealtyDocumentBean implements Serializable {

	private static final long serialVersionUID = -8486486280143002453L;
	private RealtyDocument realtyDocument;
	private RealtyDocument searchDocument;
	@ManagedProperty(value = "#{realtyDocumentService}")
	private RealtyDocumentService realtyDocumentService;
	private File file;
	private String filePath;
	private List<RealtyDocument> searchResult;
	private String url;
	private Integer editIndex;

	public RealtyDocumentBean() {
		realtyDocument = new RealtyDocument();
		searchDocument = new RealtyDocument();
	}

	public void save() {
		try {
			RealtyDocument newDocument = null;
			if (filePath != null && !filePath.equals("")) {
				file = new File(filePath);
			}
			if (editIndex != null) {
				newDocument = realtyDocumentService.updateDocument(realtyDocument, file);
				searchResult.set(editIndex.intValue(), newDocument);
			} else {
				newDocument = realtyDocumentService.addNewDocument(realtyDocument, file);
			}

			if (newDocument != null) {
				FacesUtils.addMsg("msg.lable.saved", FacesMessage.SEVERITY_INFO, "");
			} else {
				FacesUtils.addMsg("msg.lable.faild", FacesMessage.SEVERITY_ERROR, "");
			}
		} catch (Exception e) {
			FacesUtils.addMsg("msg.lable.exception", FacesMessage.SEVERITY_ERROR, "");
			e.printStackTrace();
		} finally {
			realtyDocument = new RealtyDocument();
			editIndex = null;
			file = null;
			filePath = null;
		}
	}

	public void edit(Integer index) {
		editIndex = index;
		realtyDocument = (RealtyDocument) SerializationUtils.clone(searchResult.get(index.intValue()));

	}

	public void handleFileUpload(FileUploadEvent event) {
		UploadedFile uploadedFile = event.getFile();
		FacesContext facesContext = FacesContext.getCurrentInstance();
		filePath = facesContext.getExternalContext().getRealPath("/") + "/uploads/" + uploadedFile.getFileName();
		InputStream input = null;
		OutputStream output = null;
		try {
			input = uploadedFile.getInputstream();
			output = new FileOutputStream(new File(filePath));

			IOUtils.copy(input, output);
		} catch (IOException e) {

			e.printStackTrace();
		} finally {
			IOUtils.closeQuietly(input);
			IOUtils.closeQuietly(output);
		}
		System.out.println(">>>>>>>>> Succesful >>>>>>>>>>" + event.getFile().getFileName() + " is uploaded.");
		FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
		FacesContext.getCurrentInstance().addMessage(null, msg);
	}

	public void search() {
		try {
			searchResult = realtyDocumentService.search(searchDocument, null, null, false);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void getViewerUrl(Integer Index) {
		url = realtyDocumentService.getViewerUrl(searchResult.get(Index.intValue()));
	}

	public void delete(Integer Index) {

		try {
			RealtyDocument document = searchResult.get(Index.intValue());
			realtyDocumentService.deleteDocument(document, true);
			searchResult.remove(Index.intValue());
			FacesUtils.addMsg("msg.lable.deleted", FacesMessage.SEVERITY_INFO, "");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			FacesUtils.addMsg("msg.lable.exception", FacesMessage.SEVERITY_INFO, "");
			e.printStackTrace();
		}
	}

	public RealtyDocument getRealtyDocument() {
		return realtyDocument;
	}

	public void setRealtyDocument(RealtyDocument realtyDocument) {
		this.realtyDocument = realtyDocument;
	}

	public RealtyDocumentService getRealtyDocumentService() {
		return realtyDocumentService;
	}

	public void setRealtyDocumentService(RealtyDocumentService realtyDocumentService) {
		this.realtyDocumentService = realtyDocumentService;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public List<RealtyDocument> getSearchResult() {
		return searchResult;
	}

	public void setSearchResult(List<RealtyDocument> searchResult) {
		this.searchResult = searchResult;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Integer getEditIndex() {
		return editIndex;
	}

	public void setEditIndex(Integer editIndex) {
		this.editIndex = editIndex;
	}

	public RealtyDocument getSearchDocument() {
		return searchDocument;
	}

	public void setSearchDocument(RealtyDocument searchDocument) {
		this.searchDocument = searchDocument;
	}

}
